function deploy() {

	echo "`date` [DEPLOY INFO] Copying files"
	pscp -t $TIMEOUT -h $NODES -l thiagoepdc og-vserver-install.tar.gz $REMOTE_TMP_DIR/og-vserver-install.tar.gz
}

function installVM() {

	numlines=`wc -l $CONFIG_FILE | awk ' { print $1 } '`	
	
	linenum=0
	while [ $linenum -lt $numlines ] ; do

		linenum=$(( $linenum + 1 ))
		line=`head -$linenum $CONFIG_FILE | tail -1`
	
		prop=`echo $line | grep -v '#'`
		
		#coment
		if [ -z $prop ]; then
			continue
		fi
		
		local vserverusname=`echo $prop | awk ' { print $1 } '`
		local pwdl=`echo $prop | awk ' { print $2 } '`
		local host_playpen=`echo $prop | awk ' { print $3 } '`

		#Install
		echo "`date` [DEPLOY INFO] Installing on: " $host
		ssh -l thiagoepdc $host "cd $REMOTE_TMP_DIR && tar xzvf og-vserver-install.tar.gz && cd vserver_scripts && sudo ./build-og-vserver.sh $vserverusname $pwdl $host_playpen 1> logBuild 2> logErrorBuild"		
	done
}

function usage() {

	echo "Usage: ./fullBuild.sh <nodesFile> <times-in-seconds> <config_file> <remote_tmp_dir>"
	echo "For OurGrid updates and additional information, see the "
	echo "OurGrid Project home page at http://www.ourgrid.org/ "
}

# Main
# Assign the variables
nargs=$#

if [ $nargs -eq 4 ]; then
	NODES=$1
	TIMEOUT=$2
	CONFIG_FILE=$3
	REMOTE_TMP_DIR=$4
else 
	usage
	exit 1
fi

deploy
installVM
